package com.baidu.foody.view

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import androidx.annotation.LayoutRes
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.baidu.foody.R
import com.drake.brv.utils.linear
import com.drake.brv.utils.setup

class ShimmerRecyclerView(
    context: Context,
    attrs: AttributeSet? = null
): RecyclerView(context, attrs) {
    private var mChildCount = 0
    @LayoutRes private var mShimmerLayoutId: Int = 0
    private var mAdapter: Adapter<*>? = null

    private val mShimmerAdapter: ShimmerAdapter by lazy {
        ShimmerAdapter(mShimmerLayoutId,mChildCount)
    }

    init {
        //解析自定义的属性
        context.obtainStyledAttributes(attrs, R.styleable.ShimmerRecyclerView).apply {
            //加载动画中item的个数
            mChildCount = getInt(R.styleable.ShimmerRecyclerView_shimmer_child_count, 10)
            //加载动画的布局
            mShimmerLayoutId = getResourceId(R.styleable.ShimmerRecyclerView_shimmer_layout, 0 )
            recycle()
        }
        //配置
        layoutManager = LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
    }

    fun initAdapter(adapter: Adapter<*>?){
        mAdapter = adapter
    }

    //显示加载动画
    fun showShimmer() {
        adapter = mShimmerAdapter
    }


    //隐藏加载动画
    fun hideShimmer() {
        adapter = mAdapter
    }


}